﻿using Cinch;
using CodeSHARPer.Model;
using CodeSHARPer.Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Text;

namespace CodeSHARPer.ViewModel.Dialogs
{
    public class CodePropertiesViewModel : ViewModelBase
    {
        CodeEditorOptions options;

        public CodePropertiesViewModel(CodeEditorOptions options, string workingFolderPath)
        {
            this.options = options;
            this.FolderPath = workingFolderPath;

            //commands
            this.ChooseFolderPathCommand = new SimpleCommand<object, object>(DoChooseFolderPathCommand);
        }

        public ICSharpCode.AvalonEdit.TextEditorOptions TextEditorOptions
        {
            get { return options.Options; }
            set
            {
                options.Options = value;
                NotifyPropertyChanged(ObservableHelper.GetPropertyName<CodePropertiesViewModel>(c => c.TextEditorOptions));
            }
        }

        public bool ShowLineNumbers
        {
            get { return options.ShowLineNumbers; }
            set
            {
                options.ShowLineNumbers = value;
                NotifyPropertyChanged(ObservableHelper.GetPropertyName<CodePropertiesViewModel>(c => c.ShowLineNumbers));
            }
        }

        public string SeletecFont
        {
            get { return options.EditorFont; }
            set
            {
                options.EditorFont = value;
                NotifyPropertyChanged(ObservableHelper.GetPropertyName<CodePropertiesViewModel>(c => c.SeletecFont));
            }
        }

        public int FontSize
        {
            get { return options.FontSize; }
            set
            {
                options.FontSize = value;
                NotifyPropertyChanged(ObservableHelper.GetPropertyName<CodePropertiesViewModel>(c => c.FontSize));
            }
        }

        ObservableCollection<string> fonts;
        public ObservableCollection<string> Fonts
        {
            get
            {
                if (fonts == null)
                    FindInstalledFonts();

                return fonts;
            }
            set
            {
                fonts = value;
                NotifyPropertyChanged(ObservableHelper.GetPropertyName<CodePropertiesViewModel>(c => c.Fonts));
            }
        }

        string folderPath;
        public string FolderPath
        {
            get { return folderPath; }
            set
            {
                folderPath = value;
                NotifyPropertyChanged(ObservableHelper.GetPropertyName<CodePropertiesViewModel>(c => c.FolderPath));

                //save the folder path
                if (string.IsNullOrEmpty(folderPath) == false && Directory.Exists(folderPath))
                {
                    Workspace.Instance.Config.WorkingFolderFullPath = folderPath;
                    Workspace.Instance.WorkingFolder.FolderPath = folderPath;
                }
            }
        }
        #region Commands
        public SimpleCommand<object, object> ChooseFolderPathCommand { get; set; }

        private void DoChooseFolderPathCommand(object args)
        {
            FolderBrowserViewModel vm = new FolderBrowserViewModel();
            bool? result = ServiceResolver.Instance.UIVisualizerService.ShowDialog("FolderBrowserView", vm);

            if (result.GetValueOrDefault())
                this.FolderPath = vm.FolderPath;
        }
        #endregion Commands

        #region Methods
        private void FindInstalledFonts()
        {
            InstalledFontCollection installedFontCollection = new InstalledFontCollection();

            IEnumerable<string> installedFonts =
                from f in installedFontCollection.Families
                select f.Name;

            fonts = new ObservableCollection<string>(installedFonts);
        }

        internal CodeEditorOptions GetOptions()
        {
            return options;
        }
        #endregion
    }
}
